package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.FactoryTeamEmployee;
import com.uwlaser.service.FactoryTeamEmployeeService;
import com.uwlaser.service.dto.FactoryTeamEmployeeDto;
import com.uwlaser.service.dto.FactoryTeamEmployeeQueryParam;
import com.uwlaser.service.mapper.FactoryTeamEmployeeMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author huangpan
* @date 2022-12-02
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FactoryTeamEmployeeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FactoryTeamEmployeeServiceImpl extends CommonServiceImpl<FactoryTeamEmployeeMapper, FactoryTeamEmployee> implements FactoryTeamEmployeeService {

    // private final RedisUtils redisUtils;
    private final FactoryTeamEmployeeMapper factoryTeamEmployeeMapper;

    @Override
    public PageInfo<FactoryTeamEmployeeDto> queryAll(FactoryTeamEmployeeQueryParam query, Pageable pageable) {
        IPage<FactoryTeamEmployee> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FactoryTeamEmployee> page = factoryTeamEmployeeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FactoryTeamEmployeeDto.class);
    }

    @Override
    public List<FactoryTeamEmployeeDto> queryAll(FactoryTeamEmployeeQueryParam query){
        return ConvertUtil.convertList(factoryTeamEmployeeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FactoryTeamEmployeeDto.class);
    }

    @Override
    public FactoryTeamEmployee getById(Integer id) {
        return factoryTeamEmployeeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FactoryTeamEmployeeDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), FactoryTeamEmployeeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FactoryTeamEmployeeDto resources) {
        FactoryTeamEmployee entity = ConvertUtil.convert(resources, FactoryTeamEmployee.class);
        return factoryTeamEmployeeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FactoryTeamEmployeeDto resources){
        FactoryTeamEmployee entity = ConvertUtil.convert(resources, FactoryTeamEmployee.class);
        int ret = factoryTeamEmployeeMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return factoryTeamEmployeeMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Map<String, String>> getUser() {
        return factoryTeamEmployeeMapper.getUser();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<FactoryTeamEmployeeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FactoryTeamEmployeeDto factoryTeamEmployee : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("班组ID", factoryTeamEmployee.getTeamGroupId());
              map.put("员工ID", factoryTeamEmployee.getEmployeeId());
              map.put("是否是负责人", factoryTeamEmployee.getIsPrincipal());
              map.put("创建人", factoryTeamEmployee.getCreateBy());
              map.put("创建时间", factoryTeamEmployee.getCreateTime());
              map.put("更新人", factoryTeamEmployee.getUpdateBy());
              map.put("更新时间", factoryTeamEmployee.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
